package com.xing.binbackend.pattern.strategy33.ecommerce.pay;

import java.util.HashMap;
import java.util.Map;

public class PaymentContextNew {
    private final PaymentStrategy strategy;

    // 使用Builder模式支持流畅的参数设置
    public static class Builder {
        // 用Map<String, Object>承载任意数量的参数
        private final Map<String, Object> params = new HashMap<>();
        private String strategyType;

        public Builder setStrategyType(String type) {
            this.strategyType = type;
            return this;
        }

        public Builder addParam(String key, Object value) {
            params.put(key, value);
            return this;
        }

        public PaymentContext build() {
            PaymentStrategy strategy = StrategyFactory.getStrategy(strategyType, params);
            return new PaymentContext(strategy);
        }
    }

    private PaymentContextNew(PaymentStrategy strategy) {
        this.strategy = strategy;
    }

    public void executePayment(double amount) {
        strategy.pay(amount);
    }
}
